package com.ymatou.iapi.liveordermanage.testcase;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.resp.Order;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.ymatou.iapi.livemanage.service.CloseActivityCall;
import com.ymatou.iapi.liveordermanage.parameter.UserClickReq;
import com.ymatou.iapi.liveordermanage.service.UserClickCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.SqTradingCallService;
import com.ymttest.database.model.shenqi.SqliveClickRecord;
import com.ymttest.database.model.shenqi.SqliveOrderDetail;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailPo;
import com.ymttest.database.sqlwapper.shenqi.SqliveClickRecordWrapper;
import com.ymttest.database.sqlwapper.shenqi.SqliveOrderDetailWrapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;

public class Ts_UserClick {

	private static UserClickReq userClickReq = null;
	private static UserClickCall userClickCall = null;
	
	private static SqliveClickRecordWrapper sqliveClickRecordWrapper = new SqliveClickRecordWrapper();
	
	private static long autotest_goodsOwnerId = Long.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId"));

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("神器直播统计用户点击");
	}

	@Before
	public void caseUp() {
		
		userClickReq = new UserClickReq();
		userClickCall = new UserClickCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new CloseActivityCall().getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verifySqliveClickRecord() {
		
		SqliveClickRecord sqliveClickRecord = sqliveClickRecordWrapper
				.selectByUserIdAndSellerIdAndProductId(
						userClickReq.getUserId(), userClickReq.getSellerId(),
						userClickReq.getProductId());
		
		Logger.comment("验证sqlive_click_record");

		Logger.verifyEquals(userClickReq.getLiveId(),
				Long.parseLong(sqliveClickRecord.getLive_id()), "验证liveId");
	}
	
	public void verifySqliveOrderDetail(TradingServiceResp placeOrderResp) {
		
		SqliveOrderDetailWrapper sqliveOrderDetailWrapper = new SqliveOrderDetailWrapper();
		
		for (int i = 0; i < placeOrderResp.getOrderList().size(); i++) {
			
			Order order = placeOrderResp.getOrderList().get(i);
			Long orderId = order.getOrderId();
			Long sellerId = order.getSellerId();
			
			List<SqliveOrderDetail> sqliveOrderDetailList = sqliveOrderDetailWrapper.selectByOrderId(orderId);
			List<OrderDetailPo> orderDetailPoList = placeOrderResp
					.getOrderList().get(i).getOrderDetailPoList().stream()
					.sorted(Comparator.comparing(OrderDetailPo::getCatalogId))
					.collect(Collectors.toList());

			int j = 0;
			
			for (SqliveOrderDetail actualSqliveOrderDetail : sqliveOrderDetailList) {
				
				OrderDetailPo orderDetailPo = orderDetailPoList.get(j);
				String productId = orderDetailPo.getProductId();
				
				// 取3个小时内的点击数据
				SqliveClickRecord sqliveClickRecord = sqliveClickRecordWrapper.selectIn3Hour(
						placeOrderResp.getUserId(), sellerId, productId);
				
				Logger.comment("验证sqlive_order_detail");
				
				Logger.verifyEquals(orderDetailPo.getCatalogId(), actualSqliveOrderDetail.getCatalog_id(), "验证Catalog_id");
				Logger.verifyEquals(sqliveClickRecord.getLive_id(), actualSqliveOrderDetail.getLive_id(), "验证Live_id");
				Logger.verifyEquals(orderDetailPo.getProductCount(), actualSqliveOrderDetail.getNum(), "验证Num");
				Logger.verifyEquals(productId, actualSqliveOrderDetail.getProduct_id(), "验证Product_id");
				
				j++;
			}
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UserClick_001() {
		Logger.start(true, "用户点击上报");
		try {

			userClickReq.setLiveId((long)TestDataManager.getRandomIntNum(10));
			userClickReq.setProductId("psdf1");
			userClickReq.setSellerId(582934823L);
			userClickReq.setUserId(4784385L);
			
			userClickCall.setData(userClickReq);
			userClickCall.callService();
			
			this.verifySqliveClickRecord();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UserClick_002() {
		Logger.start(true, "用户下单统计");
		try {
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int)autotest_goodsOwnerId, 0, false);
			
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserClick(productBean, autotest_goodsOwnerId, false);
			
			this.verifySqliveOrderDetail(placeOrderResp);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UserClick_003() {
		Logger.start(true, "3个小时以前统计数据，用户下单统计");
		try {
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int)autotest_goodsOwnerId, 0, false);
			
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserClick(productBean, autotest_goodsOwnerId, true);
			
			this.verifySqliveOrderDetail(placeOrderResp);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
